package com.ymk.cmd;

import com.alibaba.fastjson.JSON;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.mybatis.Pagination;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.bo.goods.GoodsCatBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.core.enums.goods.GoodsCatStatusEnum;
import com.ymk.core.enums.goods.GoodsStatusEnum;
import com.ymk.mq.MessageSender;
import com.ymk.service.IGoodsCatService;
import com.ymk.service.IGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class GoodsCatListCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsCatBO reqData = super.getData(sign, GoodsCatBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("status", reqData.getStatus())
                .addParam("name", reqData.getName())
                .addParam("mchId", reqData.getMchId())
                .addParam("loadMch", true)
                .setReturnStruct(sign.getReturnStruct());
        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<GoodsCatBO> respDataList = this.goodsCatService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsCatBO reqData = super.getData(sign, GoodsCatBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("status", reqData.getStatus())
                .addParam("name", reqData.getName())
                .addParam("mchId", sign.getMchId())
                .addParam("loadMch", true)
                .setReturnStruct(sign.getReturnStruct());

        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<GoodsCatBO> respDataList = this.goodsCatService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }


    @Override
    public CmdSign executeStore(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsCatBO reqData = super.getData(sign, GoodsCatBO.class);

        String goodsCatStr = this.redisTemplate.opsForValue().get(SysConstants.REDIS_KEY_GOODS_CAT + sign.getMchId().toString());
        if (StringUtils.isNotBlank(goodsCatStr)){
            respSign.setSource(goodsCatStr);
            return respSign;
        }

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId",sign.getMchId())
                .addParam("status", GoodsCatStatusEnum.NORMAL.getStatus());

        List<GoodsCatBO> goodsCatBOList = this.goodsCatService.list(paramsBuilder.build());
        for (GoodsCatBO goodsCatBO:goodsCatBOList){
            paramsBuilder.clearParam()
                    .addParam("catId",goodsCatBO.getId())
                    .addParam("status", GoodsStatusEnum.NORMAL.getStatus())
                    .addParam("orderBySalesVolumeDesc",true);
            List<GoodsBO> list = this.goodsService.list(paramsBuilder.build());
            goodsCatBO.setGoodsBOList(list);
        }

        this.messageSender.sendGoodsCatCacheSync(sign.getMchId(),sign);
        respSign.setSource(super.getSource(goodsCatBOList));
        return respSign;
    }


    @Override
    public String getCmdCode() {
        return GoodsCmdCode.GOODS_CAT_LIST;
    }

    @Autowired
    private IGoodsCatService goodsCatService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MessageSender messageSender;
}